package com.java.base.exercise.designmode.guanchazhe;

import java.util.*;

/**
 * @Author RenPu
 * @Date 2021/12/16 11:43
 * @Version 1.0
 * @Description: 用于管理不同的推送类型，同时保存在内存中
 **/
public class EventManager {
    Map<Enum<EventType>, List<EventListern>> listeners = new HashMap<>();

    public EventManager(Enum<EventType> ... opern) {

        for (Enum<EventType> eventTypeEnum : opern) {
            listeners.put(eventTypeEnum,new ArrayList<>());
        }

    }

    public enum EventType {
        MQ, Message
    }


    /**
     * 订阅事件
     */
   public void subscribe(Enum<EventType> eventTypeEnum,EventListern eventListern){
       List<EventListern> eventListeners = listeners.get(eventTypeEnum);
      eventListeners.add(eventListern);
   }



    /**
     * 取消订阅事件
     */
    public void unsubscribe(Enum<EventType> eventTypeEnum,EventListern eventListern){
        List<EventListern> eventListeners = listeners.get(eventTypeEnum);
        eventListeners.remove(eventListern);
    }



    /**
     * 取消订阅事件
     */
    public void notif(Enum<EventType> eventTypeEnum,LotteryResult lotteryResult){
        List<EventListern> eventListeners = listeners.get(eventTypeEnum);
        for (EventListern eventListener : eventListeners) {
            eventListener.evnlistin(lotteryResult);
        }
    }

}
